package com.example.administrator.nurse.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.administrator.nurse.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * Created by Administrator on 2015/8/25.
 *
 */

public class imageLoderHelper {

    public static void showPicWithImLoder(final ImageLoader imageLoader,final int width,final ImageView iv,final String pT,final int drawable){
        BitmapFactory.Options BOptions = new BitmapFactory.Options();
        //options.inJustDecodeBounds = false;
        BOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        BOptions.inPurgeable = true;
        BOptions.inInputShareable = true;//。当系统内存不够时候图片自动被回
        //BOptions.inSampleSize = InternetHelper.calculateInSampleSize(BOptions, width - 120, ((width - 120) * 800) / 480);
        DisplayImageOptions options;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(drawable) //设置图片在下载期间显示的图片
                .showImageForEmptyUri(drawable)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(drawable)  //设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                .considerExifParams(true)  //是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
                .decodingOptions(BOptions)//设置图片的解码配置
                        //.delayBeforeLoading(int delayInMillis)//int delayInMillis为你设置的下载前的延迟时间
                        //设置图片加入缓存前，对bitmap进行设置
                        //.preProcessor(BitmapProcessor preProcessor)
                        //.resetViewBeforeLoading(true)//设置图片在下载前是否重置，复位
                        //.displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角，弧度为多少
                .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
                .build();//构建完成
        imageLoader.displayImage(pT, iv, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                Log.d("zzzzz","load onLoadingFailed "+s+" failReason "+failReason.getType().toString());
                //showPicWithImLoder(imageLoader,width,iv,pT,drawable);
            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {

            }

            @Override
            public void onLoadingCancelled(String s, View view) {
                Log.d("zzzzz","load onLoadingCancelled "+s);
            }
        });
    }

    public static DisplayImageOptions getLoderOption(int drawable,int cicle){

        DisplayImageOptions options;
        if(cicle>0) {
            options = new DisplayImageOptions.Builder()
                    .displayer(new RoundedBitmapDisplayer(cicle))
                    .showImageOnLoading(drawable) //设置图片在下载期间显示的图片
                    .showImageForEmptyUri(drawable)//设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(drawable)  //设置图片加载/解码过程中错误时候显示的图片
                    .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                    .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                    .considerExifParams(true)  //是否考虑JPEG图像EXIF参数（旋转，翻转）
                    .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
                    .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
                    .build();//构建完成
        }else{
            options = new DisplayImageOptions.Builder()
                    .displayer(new SimpleBitmapDisplayer())
                    .showImageOnLoading(drawable) //设置图片在下载期间显示的图片
                    .showImageForEmptyUri(drawable)//设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(drawable)  //设置图片加载/解码过程中错误时候显示的图片
                    .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                    .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                    .considerExifParams(true)  //是否考虑JPEG图像EXIF参数（旋转，翻转）
                    .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
                    .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
                    /** 2016-5-5 注释 option */
                            //.delayBeforeLoading(int delayInMillis)//int delayInMillis为你设置的下载前的延迟时间
                            //设置图片加入缓存前，对bitmap进行设置
                            //.preProcessor(BitmapProcessor preProcessor)
                            //.resetViewBeforeLoading(true)//设置图片在下载前是否重置，复位
                            //.displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角，弧度为多少
                            //.displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
                    .build();//构建完成
        }
        return options;
    }

    public static DisplayImageOptions adapter(){
        DisplayImageOptions mOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading  (R.mipmap.user)     //加载过程中
                .showImageForEmptyUri(R.mipmap.user)     //uri为空时
                .showImageOnFail     (R.mipmap.user)     //加载失败时
                .cacheOnDisk(true)
                .cacheInMemory(true)                             //允许cache在内存和磁盘中
                .bitmapConfig(Bitmap.Config.RGB_565)             //图片压缩质量参数
                .build();
        return mOptions;
    }

    public static DisplayImageOptions hotLive(int drawable){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(drawable)
                .showImageForEmptyUri(drawable)
                .showImageOnFail(drawable)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();
        return options;
    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        //保证是方形，并且从中心画
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int w;
        int deltaX = 0;
        int deltaY = 0;
        if (width <= height) {
            w = width;
            deltaY = height - w;
        } else {
            w = height;
            deltaX = width - w;
        }
        final Rect rect = new Rect(deltaX, deltaY, w, w);
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        //圆形，所有只用一个

        int radius = (int) (Math.sqrt(w * w * 2.0d) / 2);
        canvas.drawRoundRect(rectF, radius, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}
